package com.system.service.impl;

import com.common.enums.BaseResponseCode;
import com.common.utils.AjaxResult;
import com.common.utils.StringUtils;
import com.system.entity.SysRoleMenuEntity;
import com.system.mapper.SysRoleMenuMapper;
import com.system.service.ISysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SysRoleMenuServiceImpl implements ISysRoleMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;


    /**
     * 数据添加
     *
     * @param sysRoleMenuEntity
     */

    @Override
    public AjaxResult sysRoleMenuAdd(SysRoleMenuEntity sysRoleMenuEntity) {
        int n = sysRoleMenuMapper.sysRoleMenuAdd(sysRoleMenuEntity);
        return new AjaxResult(BaseResponseCode.ADD_SUCCESS.getCode(), BaseResponseCode.ADD_SUCCESS.getMsg());
    }

    /**
     * 删除与批量删除
     *
     * @param ids
     */
    @Override
    @Transactional
    public AjaxResult sysRoleMenuDel(List<Long> ids) {
        sysRoleMenuMapper.sysRoleMenuDel(ids);
        return new AjaxResult(BaseResponseCode.DELETE_SUCCESS.getCode(), BaseResponseCode.DELETE_SUCCESS.getMsg());
    }

    /**
     * 更新
     *
     * @param sysRoleMenuEntity
     */
    @Override
    public AjaxResult sysRoleMenuUpById(SysRoleMenuEntity sysRoleMenuEntity) {
        sysRoleMenuMapper.sysRoleMenuUpById(sysRoleMenuEntity);
        return new AjaxResult(BaseResponseCode.UPDATE_SUCCESS.getCode(), BaseResponseCode.UPDATE_SUCCESS.getMsg());
    }

    /**
     * 查询单条数据
     */
    @Override
    public SysRoleMenuEntity sysRoleMenuGetById(String id) {
        return sysRoleMenuMapper.sysRoleMenuGetById(id);
    }


    /**
     * 查询数据库
     *
     * @return
     */
    @Override
    public AjaxResult sysRoleMenuSelectListByPage(SysRoleMenuEntity sysRoleMenuEntity) {
        Integer count = sysRoleMenuMapper.sysRoleMenuSelCount();
        List<SysRoleMenuEntity> list = sysRoleMenuMapper.sysRoleMenuSelectListByPage(sysRoleMenuEntity);
        return AjaxResult.data(count, list);
    }

    /**
     * 添加角色拥有的菜单
     */

    @Override
    public void sysRoleMenuAddList(Long userId, List<Long> t) {
        sysRoleMenuMapper.sysRoleMenuAddList(userId.longValue(), t);
    }

    /**
     * 获取该角色已选的菜单id
     *
     * @param id
     * @return
     */
    @Override
    public List<Long> getMenuId(Long id) {
        return sysRoleMenuMapper.getMenuId(id);
    }

    /**
     * 获取角色菜单权限
     *
     * @param userId
     * @return
     */
    @Override
    public Set<String> selectPermsByUserId(Long userId) {

        List<String> perms = sysRoleMenuMapper.selectPermsByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms) {
            if (StringUtils.isNotEmpty(perm)) {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

}